<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实现斐波那契数列</title>
</head>
<body>

    <script>

        //递归版本
        function fibo(n)
        {
            if(n === 0) return 0;
            if(n === 1||n === 2) return 1;

            return fibo(n-1)+fibo(n-2);
        }
       console.log(fibo(5));

       //非递归版本
       function fibo2(n)
       {
            if(n === 0) return 0;
            if(n === 1||n === 2) return 1;
            var a = 1,b = 1,c = 2;

            while(n-3>0)
            {
                a = b;
                b = c;
                n--;
                c= a+b;
            }
            console.log(c);
       }

       fibo2(8);
    </script>
    
</body>
</html>